package com.dragon.www.haier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.adapter.HaierLampAdapter;
import com.dragon.www.haier.entity.Device;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.entity.HaierDevices;
import com.dragon.www.haier.entity.HaierDevicesStatus;
import com.dragon.www.haier.entity.HaierDevicesStatusInfo;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;
import com.dragon.www.haier.view.TitleView;

import org.dragonnova.lamp.Lamp;
import org.dragonnova.lamp.LampController;

import java.util.ArrayList;
import java.util.List;

/**
 * 灯
 */
public class HaierLampActivity extends FragmentActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView listView;
    private TitleView titleView;
    private HaierLampAdapter adapter;
    private List<Device> deviceList = new ArrayList<>();
    boolean isContinue = false;

    private LampController mLampController;
    private List<Lamp> mLamps = new ArrayList<>();
    private List<HaierDevices> HaierDevicesList = new ArrayList<HaierDevices>();
    private HaierDevices mHaierDevices;
    private final Handler viewHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    refreshUI();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        HaierDevicesList = (List<HaierDevices>)intent.getSerializableExtra("list");
//        List<HashMap<String,int>> mylist = (List<HashMap<String, String>>)intent.getSerializableExtra("list");
//        HaierDevices HaierDevices = (HaierDevices)mylist.get(0);




//        mLampController = LampController.getInstance(this);
//        mLampController.setLampListener(new LampListener() {
//            @Override
//            public void onRefresh(List<Lamp> lamps) {
//                mLamps.clear();
//                mLamps.addAll(lamps);
//                adapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onError(ErrorCode error) {
//
//            }
//        });
//
//        mLampController.start();
//        mLampController.discovery();


//        List<Integer> data = new ArrayList<>();
//        data.add(1);
//        data.add(2);
//        data.remove((Integer)2);
//        ControlHaierDevices(data);
//        for (int i= 0;i < 16; i++){
//            Lamp lamp = new Lamp();
//            lamp.setStatus(0);
//            mLamps.add(lamp);
//        }
        initView();
//        initData();
    }

    public void ControlHaierDevices(List<Integer> controldata) {
        NetHelper.ControlHaierDevices(HaierLampActivity.this, 16, 10, 1, 10, controldata, new NetUtils.NetCallBack<DeviceInfo>() {
            @Override
            public void success(DeviceInfo rspData) {
                Toast.makeText(HaierLampActivity.this, "成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(HaierLampActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }, DeviceInfo.class);
    }



    /**
     * 从网络获取设备
     * 2016/11/03
     */
    public void getHaierDevicesStatus() {
        NetHelper.getHaierDevicesStatus(this, mHaierDevices.getmasterId(), mHaierDevices.getslaveId(), mHaierDevices.getdevicetype(), mHaierDevices.getcommtype(), new NetUtils.NetCallBack<HaierDevicesStatusInfo>() {
            @Override
            public void success(HaierDevicesStatusInfo rspData) {
                if (rspData.getError() == 0) {
                    //保存房间信息
                    HaierDevicesStatus HaierRelayDetailInfo = rspData.getHaierDevicesStatus();
                    List<List<Integer>> statusdata = HaierRelayDetailInfo.getstatusdata();
                    List<Integer> statusdata1 = statusdata.get(0);
                    if (mHaierDevices != null){
                        for (int i = 0; i < mHaierDevices.getremark(); i++) {
                            Lamp lamp = new Lamp();
                            lamp.setStatus(statusdata1.get(i));
                            lamp.setcommtype(mHaierDevices.getcommtype());
                            lamp.setdevicetype(mHaierDevices.getdevicetype());
                            lamp.setmasterId(mHaierDevices.getmasterId());
                            lamp.setslaveId(mHaierDevices.getslaveId());
                            mLamps.add(lamp);
                        }
                    }else{
                        Toast.makeText(HaierLampActivity.this, "获取设备信息失败", Toast.LENGTH_SHORT).show();
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(HaierLampActivity.this, "获取设备信息失败，原因", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failed(String msg) {
                Log.e("请求错误", msg);
                Toast.makeText(HaierLampActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mLampController.stop();
        isContinue = false;
    }

    public static void setDevices(List<HaierDevices> HaierDevices) {

    }

    public void initView() {
        setContentView(R.layout.activity_haier_lamp);
        listView = (ListView) findViewById(R.id.listView);
        titleView = (TitleView) findViewById(R.id.item_title);
        titleView.setTitle("灯光");
        titleView.setTextBackAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new HaierLampAdapter(this, mLamps);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        if (HaierDevicesList != null){
            mHaierDevices = HaierDevicesList.get(0);
        }
        getHaierDevicesStatus();
    }

    public void initData(){
        isContinue = false;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isContinue) {
                    viewHandler.sendEmptyMessage(0);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }).start();
    }

    /**
     * 每隔500毫秒刷新UI
     * 2016/06/01
     */
    private void refreshUI() {
        getHaierDevicesStatus();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.splitbutton:
                changLight(view);
                break;
        }
    }

    private void changLight(View view) {
        int position = listView.getPositionForView(view);
        Device device = (Device) listView.getItemAtPosition(position);
        Log.e("haier", device.getDeviceName());
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

}
